//
//  RegistAndLoginViewController.m
//  product_B
//
//  Created by zyc on 16/5/27.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "RegistAndLoginViewController.h"
#import "LoginViewController.h"
#import <ShareSDKExtension/SSEThirdPartyLoginHelper.h>

@interface RegistAndLoginViewController ()
@property (nonatomic, strong)UIButton *backButton;
@property (nonatomic,strong)UILabel *titleLabel;
@end

@implementation RegistAndLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor redColor];
    self.tabBarController.tabBar.hidden = YES;
    
    _backButton = [[UIButton alloc]initWithFrame:CGRectMake(15, 30, 30, 30)];
//    [_backButton setTitle:@"返回" forState:UIControlStateNormal];
    [_backButton setImage:[UIImage imageNamed:@"叉号.png"] forState:UIControlStateNormal];
    [_backButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_backButton];
    
    _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(kWidth/2-30, 25, 100, 50)];
    _titleLabel.text = @"登录ENO";
    _titleLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:_titleLabel];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [self.downLabel addGestureRecognizer:tap];
    
}
- (IBAction)weixinButtonAction:(UIButton *)sender {
    [SSEThirdPartyLoginHelper loginByPlatform:SSDKPlatformTypeWechat onUserSync:^(SSDKUser *user, SSEUserAssociateHandler associateHandler) {
        //在此回调中可以将社交平台用户信息与自身用户系统进行绑定，最后使用一个唯一用户标识来关联此用户信息。
        //在此示例中没有跟用户系统关联，则使用一个社交用户对应一个系统用户的方式。将社交用户的uid作为关联ID传入associateHandler。
        associateHandler (user.uid, user, user);

        [[NSUserDefaults standardUserDefaults]setObject:user.rawData[@"nickname"] forKey:@"name"];
        [[NSUserDefaults standardUserDefaults]setObject:user.rawData[@"headimgurl"] forKey:@"picture"];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"欢迎" message:user.rawData[@"nickname"] preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [self.navigationController popViewControllerAnimated:YES];
        }];
        [alert addAction:cancel];
        
    }onLoginResult:^(SSDKResponseState state, SSEBaseUser *user, NSError *error) {
        if (state == SSDKResponseStateSuccess)
        {
            NSLog(@"登陆成功");
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            NSLog(@"登陆失败");
        }
    }];
}

- (IBAction)QQButtonAction:(UIButton *)sender {
    [SSEThirdPartyLoginHelper loginByPlatform:SSDKPlatformTypeQQ onUserSync:^(SSDKUser *user, SSEUserAssociateHandler associateHandler) {
        //在此回调中可以将社交平台用户信息与自身用户系统进行绑定，最后使用一个唯一用户标识来关联此用户信息。
        //在此示例中没有跟用户系统关联，则使用一个社交用户对应一个系统用户的方式。将社交用户的uid作为关联ID传入associateHandler。
        associateHandler (user.uid, user, user);

        [[NSUserDefaults standardUserDefaults]setObject:user.rawData[@"nickname"] forKey:@"name"];
        [[NSUserDefaults standardUserDefaults]setObject:user.rawData[@"figureurl_qq_2"] forKey:@"picture"];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"欢迎" message:user.rawData[@"nickname"] preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:nil];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [self.navigationController popViewControllerAnimated:YES];
        }];
        [alert addAction:cancel];
    }onLoginResult:^(SSDKResponseState state, SSEBaseUser *user, NSError *error) {
        if (state == SSDKResponseStateSuccess)
        {
            NSLog(@"登陆成功");
            
        }else{
            NSLog(@"登陆失败");
        }
    }];
    

}
- (IBAction)phoneButtonAction:(UIButton *)sender {
    LoginViewController *loginVC = [[LoginViewController alloc]init];
    [self.navigationController pushViewController:loginVC animated:YES];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}
- (void)tapAction:(UITapGestureRecognizer *)tap{
    
}
- (void)buttonAction:(UIButton *)sender{
//    CATransition *transition = [CATransition animation];
//    transition.duration = 1.0f;
//    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//    transition.type = @"cube";
//    transition.subtype = kCATransitionFromLeft;
//    transition.delegate = self;
    
    CATransition *transition = [CATransition animation];
    transition.duration = 0.7f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = @"oglFlip";
    transition.subtype = kCATransitionFromLeft;
    transition.delegate = self;
    [self.tabBarController.view.layer addAnimation:transition forKey:nil];

    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
